<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Miners\Miner;


class UpdateMinerPayYesterday extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'miners:update-miners-payyesterday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update Miners table pay_yesterday column';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $activeMiners = Miner::where('status','active')->get();
        foreach ($activeMiners as $activeMiner) {
            $payYesterday = $activeMiner->pay_yesterday;

            $yesterday = Carbon::yesterday()->toDateString();
            $minerPayouts = $activeMiner
                ->payouts()
                ->whereDate('made_at',$yesterday)
                ->get(['amount']);
            $payYesterdayAmount = 0;
            foreach ($minerPayouts as $minerPayout) {
                //var_dump($minerPayout->amount);
                $payYesterdayAmount += $minerPayout->amount;
            }
            $activeMiner->pay_yesterday = $payYesterdayAmount;
            $activeMiner->save();
        }
    }
}
